package com.example.androidlogin;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.Toolbar;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class MainActivity extends AppCompatActivity {
    private EditText mUser, mPassWord, mLanguage;
    private Button mLogin;
    private String user, password, language, url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

        url = "http://47.107.100.125:8080/app/user/login";
        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user = mUser.getText().toString();
                password = mPassWord.getText().toString();
                language = mLanguage.getText().toString();
                submit();
            }
        });
    }

    private void submit() {
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody fromBody = new FormBody.Builder()
                .add("name", user)
                .add("password", password)
                .add("language", language)
                .build();
        Request request = new Request.Builder().url(url).post(fromBody).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String str = response.body().string();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONObject jsonObject = new JSONObject(str);
                            int errorCode = jsonObject.getInt("errorCode");
                            if (errorCode == 1) {
                                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                                startActivity(intent);
                                Toast.makeText(getApplicationContext(), "请求成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }

    private void init() {
        mUser = findViewById(R.id.ed_user);
        mPassWord = findViewById(R.id.ed_password);
        mLanguage = findViewById(R.id.ed_language);
        mLogin = findViewById(R.id.btn_login);
    }
}
